local skel = fk.CreateSkill {
  name = "emo__haocai",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__haocai"] = "拿钳好菜",
  [":emo__haocai"] = "出牌阶段限一次，你可以展示一张手牌及牌堆顶牌，若展示牌颜色均相同则<a href='emo__haocai_href'>均重铸</a>，然后你可以执行展示手牌数+1的此流程。",

  ["#emo__haocai"] = "拿钳好菜：展示1张牌和牌堆顶1张牌，若颜色相同，则重铸",
  ["emo__haocai_href"] = "均重铸：将你展示的X张牌和牌堆顶牌置入弃牌堆，然后你摸X+1张牌",
  ["#emo__haocai-show"] = "拿钳好菜：请展示 %arg 张牌，若均与牌堆顶牌颜色相同，则重铸",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#emo__haocai",
  card_num = 1,
  target_num = 0,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and table.contains(player.player_cards[Player.Hand], to_select)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local allRecast = function (top, cards)
      local link = table.connect(top, cards)
      local moves = {}
      if room:getCardArea(top[1]) == Card.DrawPile then
        table.insert(moves, {
          ids = top,
          toArea = Card.DiscardPile,
          skillName = skel.name,
          moveReason = fk.ReasonRecast,
          proposer = player,
        })
      end
      cards = table.filter(cards, function (id)
        return table.contains(player.player_cards[Player.Hand], id)
      end)
      if #cards > 0 then
        table.insert(moves, {
          ids = cards,
          from = player,
          toArea = Card.DiscardPile,
          skillName = skel.name,
          moveReason = fk.ReasonRecast,
          proposer = player,
        })
      end
      if #moves > 0 then
        room:moveCards(table.unpack(moves))
      end
      room:sendFootnote(link, {
        type = "##RecastCard",
        from = player.id,
      })
      room:broadcastPlaySound("./audio/system/recast")
      room:delay(200)
      room:sendLog{
        type = "#RecastBySkill",
        from = player.id,
        card = link,
        arg = skel.name,
      }
      if not player.dead then
        player:drawCards(#link, skel.name)
      end
    end
    local top, cards = room:getNCards(1), effect.cards
    while player:isAlive() do
      room:showCards(top)
      local color = Fk:getCardById(top[1]).color
      player:showCards(cards)
      if player.dead then return end
      if table.every(cards, function (id)
        return Fk:getCardById(id).color == color
      end) then
        allRecast(top, cards)
        if player.dead then break end
        local num = #cards + 1
        cards = room:askToCards(player, {
          min_num = num, max_num = num, include_equip = false, cancelable = true, skill_name = skel.name,
          prompt = "#emo__haocai-show:::"..num
        })
        if #cards == 0 then break end
        top = room:getNCards(1)
      else
        break
      end
    end
  end,
})

return skel
